package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.AbstractVersionEntity;
import com.moyuer.bsf.domain.model.User;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 员工提成
 *
 * @author: 袁首京<yuanshoujing@gmail.com>
 * 16/4/22
 */
@Entity
@Table(name = "user_prize")
public class UserPrize extends AbstractVersionEntity {

    @JSONField(serialize = false)
    @ManyToOne(optional = false)
    private User owner; // 员工

    @NotNull
    @Column(name = "prize_type", nullable = false)
    private Integer prizeType; // 计提类型, 10 固定金额 20 百分比

    @NotNull
    @Column(name = "prize_value", nullable = false)
    private BigDecimal prizeValue; // 计提值

    @JSONField(format = "yyyy-MM-dd")
    @Column(name = "start_date", nullable = false)
    private Date startDate = new Date(); // 生效日期

    @Column(nullable = false)
    private Integer state = 0; // -1 标记删除 0 创建 1 生效

    @JSONField(serialize = false)
    @ManyToOne
    private User creator; // 创建人

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time", nullable = false, insertable = false, updatable = false)
    private Date createTime;

    @NotNull
    @Column(name = "organ_id")
    private String organId; //机构

    @Transient
    private String ownerId;
    @Transient
    private String ownerName;

    @Transient
    private String creatorId;
    @Transient
    private String creatorName;

    @Transient
    private String ownerPortrait;

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    public Integer getPrizeType() {
        return prizeType;
    }

    public void setPrizeType(Integer prizeType) {
        this.prizeType = prizeType;
    }

    public BigDecimal getPrizeValue() {
        return prizeValue;
    }

    public void setPrizeValue(BigDecimal prizeValue) {
        this.prizeValue = prizeValue;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        if (startDate == null) {
            this.startDate = new Date();
        } else {
            this.startDate = startDate;
        }
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getOrganId() {
        return organId;
    }

    public void setOrganId(String organId) {
        this.organId = organId;
    }

    public String getOwnerId() {
        if (ownerId == null && owner != null) {
            ownerId = owner.getId();
        }

        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public String getOwnerName() {
        if (ownerName == null && owner != null) {
            ownerName = owner.getName();
        }

        return ownerName;
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public String getCreatorId() {
        if (creatorId == null && creator != null) {
            creatorId = creator.getId();
        }

        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {
        if (creatorName == null && creator != null) {
            creatorName = creator.getName();
        }

        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public String getOwnerPortrait() {
        return this.owner != null ? this.owner.getPortraitName() : this.ownerPortrait;
    }

    public void setOwnerPortrait(String ownerPortrait) {
        this.ownerPortrait = ownerPortrait;
    }
}
